from django.http import HttpResponse
from django.shortcuts import render_to_response
from s53_1156_509_6.computer.models import DetailComperterPerfect, DetailComperterDamaged



#-------------------------------------------------------------------------------------------#
#-------------------------------------------------------------------------------------------#
def computer_form(request):
    return render_to_response('computer_form.html')

def computer(request):
    if 'id_computer' in request.GET and request.GET['id_computer'] and 'last_date_repair' in request.GET and request.GET['last_date_repair'] and 'repairman' in request.GET and request.GET['repairman'] and 'status' in request.GET and request.GET['status']:
	id_computer_in = request.GET['id_computer']	
        last_date_repair_in = request.GET['last_date_repair']   
        repairman_in = request.GET['repairman']
        status_in = request.GET['status']
        data = DetailComperterPerfect.objects.all()
        for data in data : 
	    if DetailComperterPerfect.objects.filter(id_computer__icontains=id_computer_in):        
	        return HttpResponse('The COMPUTER-ID is using on laboratory now. Please check COMPUTER-ID agin.')
	    else :		    
                Report = DetailComperterPerfect.objects.create(id_computer = id_computer_in,last_date_repair = last_date_repair_in, repairman = repairman_in, status = status_in)
	        Report.save()
                return render_to_response('computer_result.html',{'DATA': Report})
    else:
        return HttpResponse('Please enter data in the blank.')


#-------------------------------------------------------------------------------------------#
#-------------------------------------------------------------------------------------------#




def computerDamaged_form(request):
    return render_to_response('computerDamaged_form.html')

def computerDamaged(request):
    if 'id_computer' in request.GET and request.GET['id_computer'] and 'Date_entry' in request.GET and request.GET['Date_entry'] and 'status_damaged' in request.GET and request.GET['status_damaged'] and 'name_user' in request.GET and request.GET['name_user']:
        id_computer_in = request.GET['id_computer']	
        Date_entry_in = request.GET['Date_entry']   
        status_damaged_in = request.GET['status_damaged']
        name_user_in = request.GET['name_user']      
        data = DetailComperterPerfect.objects.all()
        for data in data :  
            if DetailComperterPerfect.objects.filter(id_computer__icontains=id_computer_in):     
                deleteData = DetailComperterPerfect.objects.filter(id_computer__icontains=id_computer_in)
	        deleteData.delete()
                update = DetailComperterPerfect.objects.create(id_computer = id_computer_in,last_date_repair = 'waiting repair', repairman = 'no repairman', status = status_damaged_in)
                update.save()
	    else :
	        return HttpResponse('Please Check Computer-ID!!!')
        
        #--------------------Add DetailComperterDamaged--------------------#
        Report = DetailComperterDamaged.objects.create(id_computer = id_computer_in,Date_entry = Date_entry_in, status_damaged = status_damaged_in, name_user = name_user_in)
        Report.save()

        return render_to_response('computerDamaged_result.html',{'DATA': Report})
    else:
        return HttpResponse('Please Check Computer-ID!!!')
 #-------------------------------------------------------------------------------------------#
 #-------------------------------------------------------------------------------------------#




def computerRepair_form(request):
    return render_to_response('computerRepair_form.html')

def computerRepair(request):
    if 'id_computer' in request.GET and request.GET['id_computer'] and 'last_date_repair' in request.GET and request.GET['last_date_repair'] and 'repairman' in request.GET and request.GET['repairman'] and 'status' in request.GET and request.GET['status']:
        
        id_computer_in = request.GET['id_computer']	
        last_date_repair_in = request.GET['last_date_repair']   
        repairman_in = request.GET['repairman']
        status_in = request.GET['status']
	
	if DetailComperterDamaged.objects.filter(id_computer__icontains=id_computer_in):     
            updateData = DetailComperterDamaged.objects.filter(id_computer__icontains=id_computer_in)
	    updateData.delete()
	    updateData = DetailComperterPerfect.objects.filter(id_computer__icontains=id_computer_in)
	    updateData.delete()
	    Report = DetailComperterPerfect.objects.create(id_computer = id_computer_in,last_date_repair = last_date_repair_in, repairman = repairman_in, status = status_in)
	    Report.save()
            return render_to_response('computerRepair_result.html',{'DATA': Report})
	elif DetailComperterPerfect.objects.filter(id_computer__icontains=id_computer_in):
            return HttpResponse('This COMPUTER-ID is Operative')
	else :	
	    return HttpResponse('Please check COMPUTER-ID again.')
        #--------------------Add Objects--------------------#
       
    else:
        return HttpResponse('Please enter data in the blank.')









